package com.life.notes.config.interceptor;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.life.notes.common.input.BasePageQuery;
import com.life.notes.config.security.model.SysUserDetails;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 上下文处理基类
 * @author lishuqian
 * @date 2020/6/15
 */
public class BaseContextHandler {
    public static ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal<>();
    public static void set(String key, Object value) {
        Map<String, Object> map = threadLocal.get();
        if (map == null) {
            map = new HashMap<>();
            threadLocal.set(map);
        }
        map.put(key, value);
    }

    public static Object get(String key) {
        Map<String, Object> map = threadLocal.get();
        if (map == null) {
            map = new HashMap<>();
            threadLocal.set(map);
        }
        return map.get(key);
    }

    /**
     * 取用户ID
     *
     * @return
     */
    public static Integer getUserId() {
        SysUserDetails userDetails = getUser();
        if (ObjectUtil.isNotNull(userDetails)){
            return userDetails.getId();
        }
        return null ;
    }

    public static List<String> getUserRole() {
        SysUserDetails userDetails = getUser();
        if (ObjectUtil.isNotNull(userDetails)){
            return userDetails.getAuthorities().stream().map(o->o.getAuthority()).distinct().collect(Collectors.toList());
        }
        return null ;
    }

    public static void checkRole(BasePageQuery basePageQuery) {
        List<String> userRole = getUserRole();
        if (CollectionUtil.isNotEmpty(userRole)){
            long count = userRole.stream().filter(o -> "admin".equals(o)).count();
            if (count == 0){
                basePageQuery.setAdministrator("0");
                basePageQuery.setUserId(getUserId());
            }
        }
    }

    public static void checkRole(Map basePageQuery) {
        List<String> userRole = getUserRole();
        if (CollectionUtil.isNotEmpty(userRole)){
            long count = userRole.stream().filter(o -> "admin".equals(o)).count();
            if (count == 0){
                basePageQuery.put("administrator","0");
                basePageQuery.put("userId",getUserId());
            }
        }
    }

    public static SysUserDetails getUser() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (ObjectUtil.isNotNull(principal) && principal instanceof SysUserDetails){
            SysUserDetails sysUserDetails = (SysUserDetails) principal;
            return sysUserDetails;
        }
        return null ;
    }

    public static String  getHeader(String name) {
        Object value = get(name);
        String returnObjectValue = returnObjectValue(value);
        return returnObjectValue ;
    }

    private static String returnObjectValue(Object value) {
        return value == null ? null : value.toString();
    }

    public static void remove() {
        threadLocal.remove();
    }

}
